//
//  CLReadController+EffectType.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/8/23.
//

#import "CLReadController+EffectType.h"
#import "CLPageViewController.h"
#import "CLReadController+Operation.h"
@implementation CLReadController (EffectType)

/// 创建阅读视图
- (void)creatPageController:(CLReadViewController *)displayController {

    // 清理
    [self clearPageController];

    // 翻页类型
    CLEffectType effectType = [CLReadConfigure sharedInstance].effectType;

    // 创建
    if (effectType == CLEffectTypeSimulation) { // 仿真

        if (displayController == nil) { return; }

        // 创建
        NSDictionary *options = @{UIPageViewControllerOptionSpineLocationKey:@(UIPageViewControllerSpineLocationMin)};

        self.pageViewController = [[CLPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];

        // 自定义tap手势的相关代理
        self.pageViewController.aDelegate = self;

        self.pageViewController.delegate = self;

        self.pageViewController.dataSource = self;

        [self.contentView insertSubview:self.pageViewController.view atIndex:0];

        self.pageViewController.view.backgroundColor = [UIColor clearColor];

        self.pageViewController.view.frame = self.contentView.bounds;

        // 翻页背部带文字效果
        self.pageViewController.doubleSided = YES;

        [self.pageViewController setViewControllers:(displayController != nil ? @[displayController] : nil) direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    }
    else if (effectType == CLEffectTypeTranslation) { // 平移

        if (displayController == nil) { return; }

        // 创建
        NSDictionary *options = @{UIPageViewControllerOptionSpineLocationKey:@(UIPageViewControllerSpineLocationMin)};
        
        self.pageViewController = [[CLPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
        
        // 自定义tap手势的相关代理
        self.pageViewController.aDelegate = self;

        self.pageViewController.delegate = self;

        self.pageViewController.dataSource = self;

        [self.contentView insertSubview:self.pageViewController.view atIndex:0];

        self.pageViewController.view.backgroundColor = [UIColor clearColor];

        self.pageViewController.view.frame = self.contentView.bounds;
        
        [self.pageViewController setViewControllers:(displayController != nil ? @[displayController] : nil) direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];

    }else if (effectType == CLEffectTypeScroll) { // 滚动

        CLReadViewScrollController *scrollController = [[CLReadViewScrollController alloc]init];

        scrollController.vc = self;

        [self.contentView insertSubview:scrollController.view atIndex:0];

        scrollController.view.frame = self.contentView.bounds;

        scrollController.view.backgroundColor = UIColor.clearColor;

        [self addChildViewController:scrollController];

    }else{ // 覆盖 无效果

        if (displayController == nil) { return; }

        CLCoverController *coverController = [[CLCoverController alloc]init];
        self.coverController = coverController;
        self.coverController.delegate = self;
        
        [self.contentView insertSubview:coverController.view atIndex:0];

        coverController.view.frame = self.contentView.bounds;

        coverController.view.backgroundColor = UIColor.clearColor;

        [coverController setController:displayController];

        if ([CLReadConfigure sharedInstance].effectType == CLEffectTypeNo) {

            coverController.openAnimate = NO;
        }
    }

    // 记录
    self.currentDisplayController = displayController;
}

/// 清理所有阅读控制器
- (void)clearPageController {

    [self.currentDisplayController removeFromParentViewController];
    self.currentDisplayController = nil;

    if (self.pageViewController != nil) {

        [self.pageViewController.view removeFromSuperview];

        [self.pageViewController removeFromParentViewController];

        self.pageViewController = nil;
    }

    if (self.coverController != nil) {

        [self.coverController.view removeFromSuperview];

        [self.coverController removeFromParentViewController];

        self.coverController = nil;
    }

    if (self.scrollController != nil) {

        [self.scrollController.view removeFromSuperview];

        [self.scrollController removeFromParentViewController];

        self.scrollController = nil;
    }
}

/// 手动设置翻页(注意: 非滚动模式调用)
- (void)setViewController:(CLReadViewController *)displayController isAbove:(BOOL)isAbove animated:(BOOL)animated {

    if (displayController != nil) {

        // 仿真
        if (self.pageViewController != nil) {

            UIPageViewControllerNavigationDirection direction = isAbove ? UIPageViewControllerNavigationDirectionReverse : UIPageViewControllerNavigationDirectionForward;
            
            if ([CLReadConfigure sharedInstance].effectType == CLEffectTypeTranslation) { // 平移

                [self.pageViewController setViewControllers:@[displayController] direction:direction animated:animated completion:nil];
            } else { // 仿真

                [self.pageViewController setViewControllers:@[displayController,[self getReadViewBGController:displayController.recordModel targetView:displayController.view]] direction:direction animated:animated completion:nil];
            }

            return;
        }

        // 覆盖 无效果
        if (self.coverController != nil) {

            [self.coverController setController:displayController animated:animated isAbove:isAbove];

            return;
        }

        // 记录
        self.currentDisplayController = displayController;
    }
}


// MARK: -- CLCoverControllerDelegate

/// 切换结果
- (void)coverController:(CLCoverController *)coverController currentController: (UIViewController *)currentController finish:(BOOL)isFinish {
    
    // 记录
    self.currentDisplayController = (CLReadViewController *)currentController;

    // 更新阅读记录
    [self updateReadRecord:self.currentDisplayController];
}

/// 将要显示的控制器
- (void)coverController:(CLCoverController *)coverController willTransitionToPendingController:(UIViewController *)pendingController{
    
    [self.readMenu showMenu:NO];

}

/// 获取上一个控制器
- (UIViewController *)coverController:(CLCoverController *)coverController getAboveControllerWithCurrentController:(UIViewController *)currentController {
    
    return [self getAboveReadViewController];
}

/// 获取下一个控制器
- (UIViewController *)coverController:(CLCoverController *)coverController getBelowControllerWithCurrentController:(UIViewController *)currentController {
    
    return [self getBelowReadViewController];
}


// MARK: -- UIPageViewControllerDelegate

/// 切换结果
- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    
    // 记录
    self.currentDisplayController = (CLReadViewController *)pageViewController.viewControllers.firstObject;

    // 更新阅读记录
    [self updateReadRecord:self.currentDisplayController];
}

/// 准备切换
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
    
    [self.readMenu showMenu:NO];
}

// MARK: -- CLPageViewControllerDelegate

/// 获取上一页
- (void)pageViewController:(CLPageViewController *)pageViewController getViewControllerBefore:(UIViewController *)viewController {
    
    // 获取上一页
    CLReadViewController *readViewController = (CLReadViewController *)[self getAboveReadViewController];

    // 手动设置
    [self setViewController:readViewController isAbove:YES animated:YES];

    // 更新阅读记录
    [self updateReadRecord:readViewController];

    // 关闭菜单
    [self.readMenu showMenu:NO];
}

/// 获取下一页
- (void)pageViewController:(CLPageViewController *)pageViewController getViewControllerAfter:(UIViewController *)viewController {
    
    // 获取下一页
    CLReadViewController *readViewController = (CLReadViewController *)[self getBelowReadViewController];

    // 手动设置
    [self setViewController:readViewController isAbove:NO animated:YES];

    // 更新阅读记录
    [self updateReadRecord:readViewController];

    // 关闭菜单
    [self.readMenu showMenu:NO];
}

// MARK: -- UIPageViewControllerDataSource

/// 获取上一页
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBefore:(UIViewController *)viewController {
    
    if ([CLReadConfigure sharedInstance].effectType == CLEffectTypeTranslation) {// 平移
        return [self getAboveReadViewController];
    } else { // 仿真
        
        // 翻页累计
        self.tempNumber -= 1;
        
        CLReadRecordModel *recordModel = ((CLReadViewController *)viewController).recordModel;
        
        // 如果没有则从背面页面获取
        if (recordModel == nil) {
            
            recordModel = ((CLReadViewBGController *)viewController).recordModel;
        }
        
        if (labs(self.tempNumber) % 2 == 0) { // 背面

            recordModel = [self getAboveReadRecordModel:recordModel];

            return [self getReadViewBGController:recordModel];

        }else{ // 内容

            return [self getReadViewController:recordModel];
        }
        
    }

    return nil;
}

/// 获取下一页
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfter:(UIViewController *)viewController {
    
    if ([CLReadConfigure sharedInstance].effectType == CLEffectTypeTranslation) { // 平移
        
        return [self getBelowReadViewController];
        
    } else { // 仿真
        
        // 翻页累计
        self.tempNumber += 1;
        
        // 获取当前页阅读记录
        CLReadRecordModel *recordModel = ((CLReadViewController *)viewController).recordModel;
        
        // 如果没有则从背面页面获取
        if (recordModel == nil) {
            
            recordModel = ((CLReadViewBGController *)viewController).recordModel;
        }
        
        if (labs(self.tempNumber) % 2 == 0) { // 背面
            
            return [self getReadViewBGController:recordModel];
            
        }else{ // 内容
            
            recordModel = [self getBelowReadRecordModel:recordModel];
            
            return [self getReadViewController:recordModel];
        }
    }
    return nil;
}
@end
